const {ccclass, property} = cc._decorator;

interface SelectCallback {
    (itemInfo: any, isToggle: boolean): void
}

@ccclass
export default class PlateNameItem extends cc.Component {

    @property(cc.Label)
    labelName: cc.Label = null;

    _callback: SelectCallback = null;

    ts_code: string = '';

    itemInfo: any = '';

    _select: boolean = false;

    _isToggle: boolean = false;

    _isUsed: boolean = false;

    setItem (item, callback: SelectCallback, isToggle = false) {
        this._callback = callback;
        this.labelName.string = item.name;
        this._isToggle = isToggle;
        this.itemInfo = item;
        this._isUsed = true;
        this.ts_code = item.ts_code;
    }


    clearItem () {
        this._isUsed = false;
        this.node.active = false;
    }

    onBtnClick () {
        if (this._callback) {
            this._callback(this.itemInfo, this._isToggle);
        }
        if (this._isToggle) {
            this.setSelect(!this._select);
        }
    }

    setSelect (isSelect) {
        if (isSelect) {
            this.node.color = cc.color(170, 130, 130);
        } else {
            this.node.color = cc.color(255, 255, 255);
        }
        this._select = isSelect;
    }

    isSelect () {
        return this._select;
    }

    isUsed () {
        return this._isUsed;
    }

    matchText (text) {
        if (text == '') {
            this.node.active = true;
        } else {
            let name = this.itemInfo.name;
            this.node.active = (name.indexOf(text) != -1);
        }
    }

}
